import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
// 导入公共布局组件
import Layout from '@/layout'

import approvals from '@/router/modules/approvals.js'
import departments from '@/router/modules/departments'
import employees from '@/router/modules/employees.js'
import permission from '@/router/modules/permission.js'
import attendances from '@/router/modules/attendances.js'
import salarys from '@/router/modules/salarys.js'
import setting from '@/router/modules/setting.js'
import social from '@/router/modules/social.js'
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * 静态路由（不需要权限就能看的页面）| asyncRoutes 动态路由（需要有权限才能看的页面）
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const asyncRoutes = [
  departments,
  setting,
  employees,
  permission,
  approvals,
  attendances,
  salarys,
  social
]

export const constantRoutes = [
  // 登录
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // 不存在的页面会跳转到这里，显示404页面
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 员工导入
  {
    path: '/import',
    component: Layout,
    hidden: true, // 不显示到菜单
    children: [{
      path: '',
      component: () => import('@/views/import')
    }]
  },
  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    // 真正的页面
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }

  // 404 page must be placed at the end !!!
  // 之前手动写路径刷新就出现404页面（ * 已经匹配了所有路径），是因为动态路由在这之后不会生效，所以需要注释掉
  // 根据身份标识要动态添加路由进来=》不能在这里放置404页面配置
  // * 通配符 =》匹配任何不存在path路径
  // { path: '*', redirect: '/404', hidden: true }

]

// 工厂函数创建路由实例
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 暂时合并（说明：后期动态路由是要根据用户权限，动态添加的）
  // routes: [...constantRoutes, ...asyncRoutes]
  routes: [...constantRoutes]
})
// 通过工厂函数得到路由实例
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由（后续动态路由会使用）
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
